package cn.itsource.smartsystem.controller;
import cn.itsource.smartsystem.exception.GlobalException;
import cn.itsource.smartsystem.domain.User;

import cn.itsource.smartsystem.service.UserService;
import cn.itsource.smartsystem.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class LoginController {
    @Autowired
    private UserService us;
    @PostMapping("/login")
    public AjaxResult login(@RequestBody User user){
        try {
            String token = us.login(user);
            System.out.println(token);
            return AjaxResult.me().setSuccess(true).setResultObj(token);
        }catch(GlobalException ge) {
            return AjaxResult.me().setSuccess(false).setMessage(ge.getMessage());
        }catch(Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统异常，请联系管理人员");
        }
   }
}
